﻿using System;
using System.Linq;
using System.Windows;
using Labthink.PCSoft.UniversalHost4.Model.Setting;
using Labthink.PCSoft.UniversalHost4.TcpCommands;
using Labthink.PCSoft.UniversalHost4.Utility;
using Newtonsoft.Json;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class UcNetSetting
    {
        private NotifyManager _notify;
        private readonly ICommands _commands = Common.Instrument.Commands;
        public UcNetSetting()
        {
            InitializeComponent();
        }

        public string Title { get; set; } = LanguageManager.GetLanguageValue("NetSetting");

        public string PermissionCode { get; set; } = "Per_NetSetting";

        private async void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            // ReSharper disable once PossibleInvalidOperationException
            if (RbSet.IsChecked.Value)
            {
                MtbIp.Txt_OnLostFocus(null, null);
                MtbSub.Txt_OnLostFocus(null, null);
                MtbDefault.Txt_OnLostFocus(null, null);
                if (string.IsNullOrEmpty(MtbIp.Value) || string.IsNullOrEmpty(MtbSub.Value)) return;
            }
            var ip = new Ip
            {
                IpAddress = MtbIp.Value.Replace(" ", ""),
                Subnetmask = MtbSub.Value.Replace(" ", ""),
                DefaultGateway = MtbDefault.Value.Replace(" ", ""),
                EnableDhcp = RbDhcp.IsChecked ?? false
            };

            _triggerButtonsStatus(false);
            var sd = await _commands.SetRemoteIpAsync(ip);
            //MessageBox.Show(sd.ErrorMsg);
            if (sd.ErrorCode < 0)
            {
                _notify.ShowError(sd.ErrorMsg);
            }
            else
            {
                _notify.ShowSuccess(sd.ErrorMsg);
            }

            _triggerButtonsStatus(true);
        }


        private async void NetSetting_OnLoaded(object sender, RoutedEventArgs e)
        {
            var w = Window.GetWindow(this);
            _notify = NotifyManager.InstanceManager(w);

            _triggerButtonsStatus(false);
            var sd = await _commands.GetRemoteIpAsync();
            var ip = JsonConvert.DeserializeObject<Ip>(sd.Value);

            RbDhcp.IsChecked = ip.EnableDhcp;
            RbSet.IsChecked = !ip.EnableDhcp;
            if (!ip.EnableDhcp)
            {
                MtbSub.Value = ip.Subnetmask;
                MtbIp.Value = ip.IpAddress;
                MtbDefault.Value = ip.DefaultGateway;
            }
            _triggerButtonsStatus(true);
        }

        private void RbDhcp_OnChecked(object sender, RoutedEventArgs e)
        {
            if (RbDhcp.IsChecked == null) return;
            if (RbDhcp.IsChecked.Value)
            {
                MtbIp.IsEnabled = false;
                MtbSub.IsEnabled = false;
                MtbDefault.IsEnabled = false;
                MtbIp.Value = "";
                MtbSub.Value = "";
                MtbDefault.Value = "";
            }
        }

        private void RbSet_OnChecked(object sender, RoutedEventArgs e)
        {
            if (RbSet.IsChecked == null) return;
            if (RbSet.IsChecked.Value)
            {
                MtbIp.IsEnabled = true;
                MtbSub.IsEnabled = true;
                MtbDefault.IsEnabled = true;
                MtbIp.Value = "";
                MtbSub.Value = "";
                MtbDefault.Value = "";
            }
        }
        

        private void _triggerButtonsStatus(bool status)
        {
            BtnOk.IsEnabled = status;
            Loading.Visibility = status ? Visibility.Collapsed : Visibility.Visible;
        }

        private void MtbSub_OnGotFocus(object sender, EventArgs e)
        {
            if (MtbSub.IsFocused && !string.IsNullOrEmpty(MtbSub.Value))
            {
                MtbSub.Value = "255.255.255.0";
            }
        }
    }
}
